<?php

/***************************************************************\
* FILE:		/sys/formfunctions.inc
* SECTION:	core > input
* FUNCTION:	reusable functions for creating form components
* USES:		
* INCLUDES:	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

function form_create($name, $contents, $action = "", $method = "POST", $buttons = "", $encoding = "") {
	global $CONFIG;
	if (!$buttons) {
		$buttons[] = form_button("BUTTON", "submitbutton", $CONFIG['button_submit'], 0, 
				"document.$name.submitted.value=1;document.$name.submit();document.$name.submitbutton.value=\"bezig..\";");
	}
	$contents[] = form_input_hidden("submitted", 0, 1);
	return array('name' => $name, 'contents' => $contents, 'action' => ($action ? $action : $_SERVER['REQUEST_URI']), 'method' => $method, 
		     'buttons' => $buttons, 'encoding' => $encoding);
}

function form_header($title) {
	return array('type' => 'header', 'title' => $title);
}

function form_info($info, $hidden = 0, $unhidetext = 'info', $title = "") {
	return array('type' => 'info', 'info' => $info, 'hidden' => $hidden, 'title' => $title, 'unhidetext' => $unhidetext);
}

function form_input_field($title, $name, $value = "", $size = 0, $maxlength = "", $onchange = "", $readonly = 0, $password = 0, $locked_default = 0) {
	if ($_POST[$name] and !$locked_default)
		$value = stripslashes($_POST[$name]);
	return array('title' => $title, 'type' => "field", 'password' => $password, 'name' => $name, 'value' => $value, 'size' => $size, 
		     'maxlength' => $maxlength, 'onchange' => $onchange, 'readonly' => $readonly);
}

function form_input_hidden($name, $value, $locked_default = 0) {
	if ($_POST[$name] and !$locked_default)
		$value = $_POST[$name];
	return array('type' => "hidden", 'name' => $name, 'value' => $value);
}

function form_input_checkbox($title, $name, $checked = "", $onchange = "", $readonly = 0, $locked_default = 0) {
	if ($_POST[$name] and !$locked_default)
		$checked = "checked";
	return array('title' => $title, 'type' => "checkbox", 'name' => $name, 'checked' => $checked, 'onchange' => $onchange, 'readonly' => $readonly);
}

function form_input_radio($title, $name, $keys, $values, $default = "", $onchange = "", $readonly = 0, $locked_default = 0) {
	if ($_POST[$name] and !$locked_default)
		$default = $_POST[$name];
	return array('title' => $title, 'type' => "radio", 'name' => $name, 'keys' => $keys, 'values' => $values, 'default' => $default, 
		    'onchange' => $onchange, 'readonly' => $readonly);
}

function form_input_file($title, $name, $value = "", $size = 0, $onchange = "", $readonly = 0, $locked_default = 0) {
	if ($_POST[$name] and !$locked_default)
		$value = $_POST[$name];
	return array('title' => $title, 'type' => "file", 'name' => $name, 'value' => $value, 'size' => $size, 'onchange' => $onchange, 'readonly' => $readonly);
}

function form_input_select($title, $name, $options, $values, $topvalue = "", $default = "", $onchange = "", $size = 1, $readonly = 0, $locked_default = 0) {
	if ($_POST[$name] and !$locked_default)
		$default = $_POST[$name];
	return array('title' => $title, 'type' => "select", 'name' => $name, 'options' => $options, 'values' => $values, 'topvalue' => $topvalue, 
		     'default' => $default, 'onchange' => $onchange, 'size' => $size, 'readonly' => $readonly);
}

function form_input_text($title, $name, $value = "", $rows = 0, $cols = 0, $onchange = "", $readonly = 0, $locked_default = 0) {
	if ($_POST[$name] and !$locked_default)
		$value = stripslashes($_POST[$name]);
	return array('title' => $title, 'type' => "text", 'name' => $name, 'value' => $value, 'rows' => $rows, 'cols' => $cols, 'onchange' => $onchange, 
		     'readonly' => $readonly);
}

function form_input_date($title, $name, $default = array(), $onchange = "", $readonly = 0, $locked_default = 0) {
	if (($_POST[$name.'_day'] or $_POST[$name.'_month'] or $_POST[$name.'_year']) and !$locked_default)
		$default = array('day' => $_POST[$name.'_day'], 'month' => $_POST[$name.'_month'], 'year' => $_POST[$name.'_year']);
	return array('title' => $title, 'type' => "date", 'name' => $name, 'default' => $default, 'onchange' => $onchange, 'readonly' => $readonly);
}

function form_input_time($title, $name, $default = array(), $onchange = "", $readonly = 0, $locked_default = 0) {
	if (($_POST[$name.'_hours'] or $_POST[$name.'_mins']) and !$locked_default)
		$default = array('hours' => $_POST[$name.'_hours'], 'mins' => $_POST[$name.'_mins']);
	return array('title' => $title, 'type' => "time", 'name' => $name, 'default' => $default, 'onchange' => $onchange, 'readonly' => $readonly);
}

function form_button($type, $name, $value, $size = "", $onclick = "") {
	return array('name' => $name, 'type' => $type, 'value' => $value, 'size' => $size, 'onclick' => $onclick);
}

function form_confirm($string) {
	$fields = array();
	$keys = array_keys($_POST);
	$vals = array_values($_POST);
	while ($k = array_pop($keys)) {
		$v = array_pop($vals);
		$fields[] = form_input_hidden($k, $v);
	}
	$fields[] = form_input_radio($string, 'confirmed', array('YES', 'NO'), array('ja', 'nee'));
	return form_create('confirmationform', $fields);
}

?>
